package com.platform.modules.sys.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.platform.common.utils.StringUtils;
import com.platform.config.XlingConstants;
import com.platform.modules.sys.entity.SysUserTokenEntity;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.ShiroUtils;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysCaptchaService;
import com.platform.modules.sys.service.SysUserService;
import com.platform.modules.sys.service.SysUserTokenService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 登录相关
 *
 * @author 肖亮
 */
@Api(tags = "SysLoginController|用户登录")
@RestController
public class SysLoginController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	@Autowired
	private SysCaptchaService sysCaptchaService;

	
	
	/**
	 * 验证码
	 *
	 * @param response
	 *            response
	 * @param uuid
	 *            uuid
	 */
	
    @ApiOperation(value="验证码" ,notes = "验证码")
	@PostMapping("captcha.jpg")
	public void captcha(HttpServletResponse response, String uuid) throws IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");
		// 获取图片验证码
		BufferedImage image = sysCaptchaService.getCaptcha(uuid);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}

	/**
	 * 登录
	 *
	 * @param form
	 *            登录表单
	 * @return RestResponse
	 */
    @ApiOperation(value="用户登录" ,notes = "用户登录")
	@SysLog("登录")
	@PostMapping("/sys/login")
	public RestResponse login(String userName, String password,HttpServletRequest request) {
		// boolean captcha = sysCaptchaService.validate(form.getUuid(),
		// form.getCaptcha());
		// if (!captcha) {
		// return RestResponse.error("验证码不正确");
		// }
    	HttpSession session=request.getSession();
		// 用户信息
		SysUserEntity user = sysUserService.queryByUserName(userName);

		// 账号不存在、密码错误
		if (user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
			return RestResponse.error("账号或密码不正确");
		}
		// 账号锁定
		if (!AptitudeEnum.aptitudeStatus.YBSYPZ.getKey().equals(user.getStatus())) {
			return RestResponse.error("账号已被锁定或未生效,请联系管理员");
		}
		// 生成token，并保存到数据库
		String token = sysUserTokenService.createToken(user.getUserId(), XlingConstants.CLIENT_PC);
		session.setAttribute("user", user);
		
		return RestResponse.success().put("token", token).put("expire", Constant.EXPIRE).put("user", user);
	}

	/**
	 * 退出系统
	 *
	 * @return RestResponse
	 */
    @ApiOperation(value="用户退出系统" ,notes = "用户退出系统")
	@PostMapping("/sys/logout")
	public RestResponse logout(String token) {
        sysUserTokenService.logout(getUserId(), token);
		return RestResponse.success();
	}

}
